package com.fick.filter;

import java.io.IOException;
import java.util.Date;
import java.util.logging.LogRecord;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.fick.hibernate.dao.VisitInfoDAO;
import com.fick.hibernate.model.VisitInfo;

public class VisitCountFilter implements Filter, java.util.logging.Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		if (req.getSession().isNew() && req.getRequestURL().toString().contains(".jsp")) {
			VisitInfoDAO dao = new VisitInfoDAO();
			VisitInfo visinfo = new VisitInfo();
			visinfo.setAddtime(new Date());
			visinfo.setIp(request.getRemoteAddr());
			visinfo.setUrl(req.getRequestURL().toString());
			dao.save(visinfo);
		}
		chain.doFilter(request, response);

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

	@Override
	public boolean isLoggable(LogRecord record) {
		return false;
	}

}
